Programming Assignment Solution Video

您所在的位置:网站首页 python programming scripting Programming Assignment Solution Video

Programming Assignment Solution Video

2022-11-06 15:06| 来源: 网络整理| 查看: 265

Welcome back to the third course in the Python scripting for DevOps specialization. In this video, I want to think about week 1's programming assignment, so let's take a look. Here it says we're going to write a program that reads in integers from a text file, and the text file is called inData.txt, and that file should be in the same directory as the executing program. When we're all done, we want to sum up those numbers with a comma separator in two digits. For example, if we saw 10, 1000, 20 in this inData.txt file, we would print 1,030.00. We should use the lab.py file provided in the lab. Let's take a look, I'm going to click, "Work in Browser". You'll see on the left-hand side, I've got the lab.py and I've got an example inData.txt file. First off I'm going to use the open method, I'll create a variable F, and I'm just going to open for reading, so that's the default, I don't have to say, "The mode," I'll just say, "InData.txt." That's good enough. I want a variable sum, I'm going to sum up the values, and now I can just loop through I'll say, "For line in F," and I'll indent and I'm going to say, "Sum plus equals," and I want to convert to a float so when I print it I can get those two decimal places. Now I said that they're integers, but I can convert an integer to a float easily. I'll just convert line to a float, add it to sum. Wow, this is a fast one, and now I'm just going to print it. I'll use that format method I've used earlier. I'm going to say the 0:3.2f, that means the floating point, and I'll say, "Format," and I'm going to pass it in sum. Like before, I should test this before I submit it. I'm going to say, "Terminal, New terminal", and I'll say, "Python lab.py." There we go, I think that's right. I can always use a source, I can look back and say, "Oh, it doesn't add up." Yes, it does. I'm going to go back to lab.py, hit the "Submit assignment" and let it submit it up there. See you next time.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3